/*
 * This file is part of Formin Media Library
 * 
 */
package formin.mediaLibrary.gui.components;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 
 * 
 * @author formin
 *
 */
public class DetailsTVPanel extends JPanel implements ActionListener, ListSelectionListener {
	//==========================================================================
    // Static attributes
    //==========================================================================
	/*
	 * used for button text and the event identifier
	 */
	private static final String ADD_COMMAND = "Add";
	private static final String REMOVE_COMMAND = "Remove";
	//==========================================================================
    // Attributes
    //==========================================================================
	
	/*
	 * pathsPanel contains 
	 * 	- a lables "Paths" at the top
	 * 	- a scrolling pane with a list displaying the paths to directories
	 * 		= selecting a path from the list enables the remove button in the buttonsPanel
	 */
	private JPanel pathsPanel;
	
	private JLabel pathsLabel;
	private JList pathsList;
	private JScrollPane pathsScrollPane;
	
	private DefaultListModel pathsListModel;
	
	/*
	 * buttonsPanel contains
	 * 	- add button that displays a folder selector dialog to add a new path
	 * 	- remove button that removes the selected path from the pathsListModel and pathsList
	 * 		= remove is disabled when there is no selection in pathsList
	 * 		= remove is enabled once a path is selected from pathsList in the pathsPanel
	 */
	private JPanel buttonsPanel;
	
	private JButton addPathButton;
	private JButton removePathButton;
	
	private JFileChooser chooser;
	
    //==========================================================================
    // Constructors
    //==========================================================================
	public DetailsTVPanel(){
		createComponents();
		chooser = new JFileChooser();
	    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	   


		//create some temp data for testing
		pathsListModel.addElement("Test1");
		pathsListModel.addElement("2432423423Test1");
		pathsListModel.addElement("C: vvvvTevvvvtv1");
				
		setupComponents();
		addComponents();	
	}

    //==========================================================================
    // Getters
    //==========================================================================
	public String[] getPathsFromList(){
		String[] tempString = new String[pathsListModel.getSize()];
		for(int i = 0; i < pathsListModel.getSize();  i++){
			tempString[i] = pathsListModel.getElementAt(i).toString();
		}
		return tempString;
	}
	
	
    //==========================================================================
    // Setters
    //==========================================================================
	public void addPathToGUI(String path){
		pathsListModel.addElement(path);
	}
	
	
    //==========================================================================
    // Inherited methods
    //==========================================================================
	@Override
	public void actionPerformed(ActionEvent event) {
		//System.out.println("DetailsTVPanel.actionPreformed listener " + event.getActionCommand());
		if(event.getActionCommand() == REMOVE_COMMAND){
			pathsListModel.remove(pathsList.getSelectedIndex());
			System.out.println("need to update sql server with new path list after remove");
			removePathButton.setEnabled(false);
			
		} else if(event.getActionCommand() == ADD_COMMAND){
			
		    int returnVal = chooser.showOpenDialog(this);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		    	pathsListModel.addElement(chooser.getSelectedFile().getAbsolutePath());
		    	
		    	//writeDataBaseTVPaths();
		    	System.out.println("need to add to gui and database");
		    }
		    
		    
		}
		
	}
	
	@Override
	public void valueChanged(ListSelectionEvent e) {
	    if (e.getValueIsAdjusting() == false) {
	    	removePathButton.setEnabled(true);
	    }
	}
    //==========================================================================
    // Static methods
    //==========================================================================
	private void createComponents(){
		
		pathsPanel = new JPanel();
		
		pathsListModel = new DefaultListModel();
		pathsLabel = new JLabel("Paths:");
		pathsList = new JList(pathsListModel);
		pathsScrollPane = new JScrollPane(pathsList);
		
		
		buttonsPanel = new JPanel();
		
		addPathButton = new JButton(ADD_COMMAND);
		removePathButton = new JButton(REMOVE_COMMAND);
		
	}
	
	private void setupComponents(){
		
		pathsPanel.setLayout(new BoxLayout(pathsPanel, BoxLayout.Y_AXIS));
		
		pathsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		pathsList.addListSelectionListener(this);
		pathsScrollPane.setPreferredSize(new Dimension(300,150));
		
		
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
		
		addPathButton.addActionListener(this);
		removePathButton.addActionListener(this);
		removePathButton.setEnabled(false);
	}
	
	private void addComponents(){
		pathsPanel.add(pathsLabel);
		pathsPanel.add(pathsScrollPane);
		
		buttonsPanel.add(addPathButton);
		buttonsPanel.add(removePathButton);
		
		add(pathsPanel);
		add(buttonsPanel);	
	}
}
